<template>
  <el-form ref="deptForm" :rules="rules" :model="form" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in employees"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// 导入获取员工信息的函数
import { getEmployeeSimple } from '@/api/employees'
import {
  addDepartments,
  getDepartDetail,
  updateDepartments
} from '@/api/department'
export default {
  props: {
    pid: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    list: {
      required: true,
      type: Array
    }
  },
  data() {
    // 校验编码
    const validaCode = (rule, value, callback) => {
      let newList = this.list.map(item => item.code)
      if (this.isEdit) {
        const curDep = this.list.find(item => item.id === this.pid)
        // console.log(curDep);
        const curCode = curDep.code
        newList = newList.filter(item => item !== curCode)
        // console.log(newList)
      }
      if (newList.includes(value)) {
        callback(new Error('当前编码已被占用'))
      } else {
        callback()
      }
    }
    // 校验名称
    const validaName = (rule, value, callback) => {
      let resList = this.list.filter(item => item.pid === this.pid).map(item => item.name) // 找到当前点击的所有儿子元素的名字
      if (this.isEdit) {
        const pid = this.list.find(item => item.id === this.pid).pid // 拿到当前的pid
        resList = this.list.filter(item => item.pid === pid && item.id !== this.pid).map(item => item.name)
      }
      if (resList.includes(value)) {
        callback(new Error('部门名称重复'))
      } else {
        callback()
      }
    }

    return {
      // 保存员工信息
      employees: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validaName, trigger: 'blur' }

        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validaCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.loadEmployee()
    this.loadDetail()
  },
  methods: {
    // 获取员工信息
    async loadEmployee() {
      const res = await getEmployeeSimple()
      console.log('员工信息', res)
      this.employees = res.data
    },
    // 点击确定
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          // 表单校验  添加功能
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async doAdd() {
      const obj = { ...this.form, pid: this.pid }
      console.log(obj)
      await addDepartments(obj)
      this.$emit('success')
    },
    async loadDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.pid)
        console.log('现在是编辑,要去获取详情', res)
        this.form = res.data
      }
    },
    // 点击取消
    hCancel() {
      this.$emit('close')
    },
    resetForm() {
      this.$refs.deptForm.resetFields()
      console.log('表单清空了') // 重置并清空表单
    },
    async doEdit() {
      await updateDepartments(this.form)
      this.$emit('success')
    }
  }
}
</script>
